#include "Mario.h"

const float offset = 0.01f;
const float jumpoffset = 1.f;
const float gravity = 20.f;

CMario::CMario(void)
{
}

CMario::CMario(float x, float y, int Width, int Height, float vx, float vy, int deltaTime, LPD3DXSPRITE spriteHandler, LPDIRECT3DTEXTURE9 image):
	MovingObject(x, y, Width, Height, vx, vy, deltaTime, spriteHandler, image)
{
	this->_sprite = new Sprite("MarioDefault",spriteHandler,Width,Height,1,1,image,10);
}

CMario::CMario(float x, float y, float vx, float vy, int deltaTime,int resourceName, CResourceManager *rm):MovingObject(x,y,vx,vy,deltaTime,resourceName,rm)
{
	_maxvx = 0.3f;
	_lastDir = DIR_RIGHT;

	_isStanding = false;

	_spriteStandRight = _rm->getSprite(CResourceManager::MARIOSTANDRIGHT1);
	_spriteMoveLeft = _rm->getSprite(CResourceManager::MARIOMOVELEFT1);
	_spriteMoveRight = _rm->getSprite(CResourceManager::MARIOMOVERIGHT1);
	_spriteJumpLeft = _rm->getSprite(CResourceManager::MARIOJUMPLEFT1);
	_spriteJumpRight = _rm->getSprite(CResourceManager::MARIOJUMPRIGHT1);
	_spriteStandLeft = _rm->getSprite(CResourceManager::MARIOSTANDLEFT1);
	
}

CMario::~CMario(void)
{
}

void CMario::Move()
{
		_lastX = _x;
		_lastY = _y;

		int w,h;
		_sprite->getSize(w,h);
		float t1 = this->getLastTranslateVector().getY();
		float t2 = this->getVelocityVector().getY();
		float ty = t1 + t2;

		if (_isJumping != true && ((_y >= 600 - h && _vy >= 0)|| (this->getLastTranslateVector() != Vector() &&(ty<=0))))
		{
			_vy = 0;
		}
		else
			if (_vy < 3.f)
   				_vy += 0.15f;

		_x= _x + (_vx)*_deltaTime;
		_y= _y + (_vy)*_deltaTime;

		if (_x < 400)
			CScreen::setPosition(0,0);
		else
			CScreen::setPosition(_x - 400,0);

		int status = this->getStatus();

		switch(status)
		{ 
			case IS_STANDING:
				{
					if (_lastDir == DIR_LEFT)
						_sprite = _spriteStandLeft;
					else 
						_sprite = _spriteStandRight;
					break;
				}
			case IS_MOVING_LEFT:
				{
					_sprite = _spriteMoveLeft;
					break;
				}
			case IS_MOVING_RIGHT:
			{
				_sprite = _spriteMoveRight;
				break;
			}
			case IS_FLYING:
				{
					if (_lastDir == DIR_LEFT)
						_sprite = _spriteJumpLeft;
					else 
						_sprite = _spriteJumpRight;
					break;
				}
		} 
	//_isMoving = false;	
		_vx = 0;
		_lastTranslateVector = Vector();
		if (_vy > 0) _isJumping = false;
}

void CMario::MoveLeft()
{
	
	_vx = -0.5f;
	_isMoving = true;
}

void CMario::MoveRight()
{

	_vx = 0.5f;
	_isMoving = true;

}

void CMario::Render()
{
	float sx,sy;
	CScreen::getPosition(sx,sy);
	this->_sprite->Render(this->_x - sx,this->_y - sy);
}

void CMario::checkSpeed()
{
	/*if (!_isMoving)
	{
		_vx = 0;
		if (_isMoveLeft && !(_vy != 0) )
			_sprite= _rm->getSprite(CResourceManager::MARIOSTANDLEFT1);
		else 
			if (_isMoveRight && !(_vy != 0))
				_sprite= _rm->getSprite(CResourceManager::MARIOSTANDRIGHT1);
	}
	_isMoving = false;
	if (!_isJumping)
		_vy = 0;
	else 
	{
		if (_vy < 0) _vy += 0.25f;
	}
	_isJumping = false;*/
}

void CMario::Jump()	
{
	
	int status = this->getStatus();
	if (_vy == 0)
	{
		_vy = -2.f;
		_isJumping = true;
	}
}

void CMario::fallDown()
{
	
}

int CMario::getStatus()
{
	if (abs(_lastY - _y) >= 1.8f)
		return IS_FLYING;
	else
		if (_vx > 0)
		{
			_lastDir = DIR_RIGHT;
			return IS_MOVING_RIGHT;
		}
		else if (_vx < 0)
			{
				_lastDir = DIR_LEFT;
				return IS_MOVING_LEFT;
			}
			else return IS_STANDING;
}

RECT CMario::getRect()
{
	int width,height;
	_sprite->getSize(width,height);

	RECT rec;
	rec.left = _x;
	rec.top = _y;
	rec.right = rec.left + width;
	rec.bottom = rec.top + height;

	return rec;
}